package top.fengyulou.xiaofeng.controller.admin;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import top.fengyulou.xiaofeng.common.Jr;
import top.fengyulou.xiaofeng.common.UserUtils;
import top.fengyulou.xiaofeng.dto.ClassifyDto;
import top.fengyulou.xiaofeng.service.ClassifyService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 分类管理
 * @author  guochao
 * @date  2022/2/2 18:40
 * @version 1.0
 */
@Controller
public class AdminClassifyController {

    @Resource
    private ClassifyService classifyService;

    //树列表
    @RequestMapping("/admin/classify/tree/list")
    public String treeList(ClassifyDto classifyDto,Model model) {
        model.addAttribute("query",classifyDto);
        return "admin/classify/admin-classify-tree-list";
    }

    //表格列表
    @RequestMapping("/admin/classify/table/list")
    public String tableList(ClassifyDto classifyDto,Model model) {
        model.addAttribute("query",classifyDto);
        return "admin/classify/admin-classify-tree-query";
    }

    @RequestMapping("/admin/classify/list")
    public String list() {
        return "admin/classify/admin-classify-list";
    }

    /**
     * 列表
     */
    @RequestMapping("/admin/classify/ajax/list")
    @ResponseBody
    public Jr ajaxList(@RequestBody ClassifyDto classifyDto) {
        classifyService.listPage(classifyDto);
        return Jr.yesd(classifyDto);
    }

    /**
     * 试卷列表
     */
    @RequestMapping("/admin/classify/ajax/all")
    @ResponseBody
    public Jr ajaxAll() {
        List<ClassifyDto> list = classifyService.list(null);
        return Jr.yesd(list);
    }

    /**
     * 试卷列表
     */
    @RequestMapping("/admin/classify/ajax/tree")
    @ResponseBody
    public Jr ajaxTree(@RequestParam String paperId) {
        ClassifyDto query=new ClassifyDto();
        query.setPaperId(paperId);
        List<ClassifyDto> list = classifyService.listTree(query);
        return Jr.yesd(list);
    }

    //编辑页面
    @RequestMapping("/admin/classify/save")
    public String save(String id, Model model) {
        if (StringUtils.isNotBlank(id)) {
            ClassifyDto classify = classifyService.getById(id);
            model.addAttribute("data", classify);
        }
        return "admin/classify/admin-classify-save";
    }

    //编辑页面
    @RequestMapping("/admin/classify/tree/save")
    public String treeSave(String id, Model model) {
        if (StringUtils.isNotBlank(id)) {
            ClassifyDto classify = classifyService.getById(id);
            model.addAttribute("data", classify);
        }
        return "admin/classify/admin-classify-tree-save";
    }

    /**
     * 保存
     */
    @RequestMapping("/admin/classify/ajax/save")
    @ResponseBody
    public Jr ajaxSave(HttpServletRequest request, ClassifyDto classifyDto) {
        classifyDto.setCurrentUserId(UserUtils.getUserId(request));
        classifyService.save(classifyDto);
        return Jr.yesd(classifyDto);
    }

    /**
     * 删除
     */
    @RequestMapping("/admin/classify/ajax/delete")
    @ResponseBody
    public Jr ajaxDelete(@RequestParam String[] ids) {
        classifyService.delete(ids);
        return Jr.yes();
    }

    //查询子节点
    @RequestMapping("/admin/classify/ajax/child")
    @ResponseBody
    public Jr ajaxDelete(@RequestParam(required = false,defaultValue = "00000000") String parentId) {
        List<ClassifyDto> list = classifyService.getChildListByParentId(parentId);
        return Jr.yesd(list);
    }
}
